package com.hello.pet.common.exception;

import com.hello.pet.common.result.ComResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@ControllerAdvice
@Slf4j
@ResponseBody
public class MyControllerAdvice {

    @ExceptionHandler(BaseException.class)
    public ComResult myControllerAdvice(HttpServletRequest request, BaseException baseException, HandlerMethod handlerMethod){
        String errorMessage = baseException.getMessage();
        String classPath = handlerMethod.getBean().getClass().getName();
        String methodName = handlerMethod.getMethod().getName();
        StringBuffer requestURL = request.getRequestURL();
        log.error("-----------------------------------error start-----------------------------------");
        log.error("data: "+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
        log.error("message: "+errorMessage);
        log.error("classPath: "+classPath);
        log.error("method: "+methodName);
        log.error("url: "+requestURL);
        log.error("-----------------------------------error end-----------------------------------",baseException);
        return ComResult.failed(baseException.getMessage());
    }
}
